
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "paramCamera.h"

#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QGroupBox>

#include "ICamera.h"
#include "Scene.h"
#include "CameraEye.h"
#include "CameraAt.h"
#include "CameraUp.h"

//#include "commonParameter.h"

//constructor
paramCamera::paramCamera(QWidget* parent)
{
	//Title
	_title = new QLabel("CAMERA");

	//Eye
	QGroupBox* groupEye = new QGroupBox("Eye", this);
	_eyeXLabel = new QLabel("x=", 0);
	_eyeYLabel = new QLabel("y=", 0);
	_eyeZLabel = new QLabel("z=", 0);
	_eyeXLineEdit = new QLineEdit();
	_eyeYLineEdit = new QLineEdit();
	_eyeZLineEdit = new QLineEdit();
	QGridLayout* eyeLayout = new QGridLayout();
	eyeLayout->addWidget(_eyeXLabel, 1, 0, 1, 1);
	eyeLayout->addWidget(_eyeXLineEdit, 1, 1, 1, 1);
	eyeLayout->addWidget(_eyeYLabel, 2, 0, 1, 1);
	eyeLayout->addWidget(_eyeYLineEdit, 2, 1, 1, 1);
	eyeLayout->addWidget(_eyeZLabel, 3, 0, 1, 1);
	eyeLayout->addWidget(_eyeZLineEdit, 3, 1, 1, 1);
	groupEye->setLayout(eyeLayout);

	//At
	QGroupBox* groupAt = new QGroupBox("At", this);
	_atXLabel = new QLabel("x=", 0);
	_atYLabel = new QLabel("y=", 0);
	_atZLabel = new QLabel("z=", 0);
	_atXLineEdit = new QLineEdit();
	_atYLineEdit = new QLineEdit();
	_atZLineEdit = new QLineEdit();
	QGridLayout* atLayout = new QGridLayout();
	atLayout->addWidget(_atXLabel, 1, 0, 1, 1);
	atLayout->addWidget(_atXLineEdit, 1, 1, 1, 1);
	atLayout->addWidget(_atYLabel, 2, 0, 1, 1);
	atLayout->addWidget(_atYLineEdit, 2, 1, 1, 1);
	atLayout->addWidget(_atZLabel, 3, 0, 1, 1);
	atLayout->addWidget(_atZLineEdit, 3, 1, 1, 1);
	groupAt->setLayout(atLayout);

	//Up
	QGroupBox* groupUp = new QGroupBox("Up", this);
	_upXLabel = new QLabel("x=", 0);
	_upYLabel = new QLabel("y=", 0);
	_upZLabel = new QLabel("z=", 0);
	_upXLineEdit = new QLineEdit();
	_upYLineEdit = new QLineEdit();
	_upZLineEdit = new QLineEdit();
	QGridLayout* upLayout = new QGridLayout();
	upLayout->addWidget(_upXLabel, 1, 0, 1, 1);
	upLayout->addWidget(_upXLineEdit, 1, 1, 1, 1);
	upLayout->addWidget(_upYLabel, 2, 0, 1, 1);
	upLayout->addWidget(_upYLineEdit, 2, 1, 1, 1);
	upLayout->addWidget(_upZLabel, 3, 0, 1, 1);
	upLayout->addWidget(_upZLineEdit, 3, 1, 1, 1);
	groupUp->setLayout(upLayout);

	//widget's layout
	_layout = new QGridLayout();

	_layout->addWidget(_title, 0, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupEye, 1, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupAt, 2, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupUp, 3, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(new QWidget(), 4, 0, 1, 2);
	_layout->setRowStretch(4, 1);

	setLayout(_layout);

	//connection to change the position of the eye
	connect(_eyeXLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectEyePosition()));
	connect(_eyeYLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectEyePosition()));
	connect(_eyeZLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectEyePosition()));

	//connection to change the position of the at
	connect(_atXLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectAtPosition()));
	connect(_atYLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectAtPosition()));
	connect(_atZLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectAtPosition()));

	//connection to change the position of the up
	connect(_upXLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectUpPosition()));
	connect(_upYLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectUpPosition()));
	connect(_upZLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectUpPosition()));
}

	//destructor
paramCamera::~paramCamera()
{}

	//fill all the widgets with values
void paramCamera::fillWidgets(ICamera* c)
{
	//fill eye
	CameraEye* eye = SCENE.getCamera();
	_eyeXLineEdit->setText(QString::number(eye->getTranslate().x));
	_eyeYLineEdit->setText(QString::number(eye->getTranslate().y));
	_eyeZLineEdit->setText(QString::number(eye->getTranslate().z));
	
	//fill at
	ICamera* at = (ICamera*)eye->getAtElement();
	_atXLineEdit->setText(QString::number(at->getTranslate().x));
	_atYLineEdit->setText(QString::number(at->getTranslate().y));
	_atZLineEdit->setText(QString::number(at->getTranslate().z));

	//fill up
	ICamera* up = (ICamera*)eye->getUpElement();
	_upXLineEdit->setText(QString::number(up->getTranslate().x));
	_upYLineEdit->setText(QString::number(up->getTranslate().y));
	_upZLineEdit->setText(QString::number(up->getTranslate().z));
}

//slot to set the position of the position widget
void paramCamera::setPosition(float x, float y, float z)
{
	//fill eye
	CameraEye* eye = SCENE.getCamera();
	_eyeXLineEdit->setText(QString::number(eye->getTranslate().x));
	_eyeYLineEdit->setText(QString::number(eye->getTranslate().y));
	_eyeZLineEdit->setText(QString::number(eye->getTranslate().z));
	
	//fill at
	ICamera* at = (ICamera*)eye->getAtElement();
	_atXLineEdit->setText(QString::number(at->getTranslate().x));
	_atYLineEdit->setText(QString::number(at->getTranslate().y));
	_atZLineEdit->setText(QString::number(at->getTranslate().z));

	//fill up
	ICamera* up = (ICamera*)eye->getUpElement();
	_upXLineEdit->setText(QString::number(up->getTranslate().x));
	_upYLineEdit->setText(QString::number(up->getTranslate().y));
	_upZLineEdit->setText(QString::number(up->getTranslate().z));
}

//set the eye position
void paramCamera::setObjectEyePosition()
{
	SCENE.getCamera()->setTranslate(CVector3f(_eyeXLineEdit->text().toFloat(),
		_eyeYLineEdit->text().toFloat(),
		_eyeZLineEdit->text().toFloat()));
}

//set the at position
void paramCamera::setObjectAtPosition()
{
	SCENE.getCamera()->getAtElement()->setTranslate(CVector3f(_atXLineEdit->text().toFloat(),
		_atYLineEdit->text().toFloat(),
		_atZLineEdit->text().toFloat()));
}

//set the up position
void paramCamera::setObjectUpPosition()
{
	SCENE.getCamera()->getUpElement()->setTranslate(CVector3f(_upXLineEdit->text().toFloat(),
		_upYLineEdit->text().toFloat(),
		_upZLineEdit->text().toFloat()));
}